/*
 *	Some Imports. Don't know if they are still all necessary.
 */


if(info.version.charAt(0)<5){
Components.utils.import("resource://app/modules/errUtils.js"); // not comp w TB5
Components.utils.import("resource://app/modules/gloda/utils.js"); //not tb5
Components.utils.import("resource://app/modules/gloda/gloda.js"); //not tb5
}
Components.utils.import("resource://gre/modules/iteratorUtils.jsm");
Components.utils.import("resource:///modules/MailUtils.js");
Components.utils.import("resource:///modules/gloda/mimemsg.js");
Components.utils.import("resource:///modules/gloda/noun_mimetype.js");
if(info.version.charAt(0)>=5){
Components.utils.import("resource://gre/modules/AddonManager.jsm"); //needed!
Components.utils.import("resource://gre/modules/Services.jsm");
}


/**
* This function encrypts the content of the event/task
*/
function encryptContent(){
	var win = Components.classes["@mozilla.org/appshell/window-mediator;1"].
	getService(Components.interfaces.nsIWindowMediator).
	getMostRecentWindow("mail:3pane");
	
    // get the content of the event/taks
	var event_title = document.getElementById('item-title').value;
	var event_description = document.getElementById('item-description').value;
	
	// dummy vars to see if the content is already encrypted
	var startEncryptedContent = "-----BEGIN PGP MESSAGE-----";
	var endEncryptedContent = "-----END PGP MESSAGE-----";
	var startEncryptedTitel = "-----SUBJECT ENCRYPTED-----";
	
	// check if the event/task is already encrypted
	if (event_description.indexOf(startEncryptedContent) != -1 && event_description.indexOf(endEncryptedContent) != -1 && event_title.indexOf(startEncryptedTitel) != -1){
		var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
	    prompts.alert(null, stringBundlef.getString("cryptoBird.compose.infowindowtitle"),stringBundlef.getString("cryptoBird.calendar.infoEncEvent"));
	}
	
	var pubringPath = Services.prefs.getCharPref('cryptoBird.pgp_path');
    var pubringFile = Services.prefs.getCharPref('cryptoBird.pgp_pk_name');
	var userIDs = new Array();
	userIDs[0] = "alice@cdc.informatik.tu-darmstadt.de";
	
	// check if title is encrypted if not, then encrypt
	if (event_title.indexOf(startEncryptedTitel) == -1){
		myDump('encrypt title');
		var enc_event_title = '-----SUBJECT ENCRYPTED-----'+win.TheChasingDragon.cryptoBirdJava.encryptSubject(userIDs, event_title, pubringPath, pubringFile);
		document.getElementById('item-title').value = enc_event_title;
	}
	
	// check if content is encrypted if not, then encrypt
	if (event_description.indexOf(startEncryptedContent) == -1 && event_description.indexOf(endEncryptedContent) == -1) {
	    myDump('encrypt content');
	    var enc_event_description = win.TheChasingDragon.cryptoBirdJava.encryptContent(userIDs, event_description, pubringPath, pubringFile);
	    document.getElementById('item-description').value = enc_event_description;
	}
	
}

function onPlainEventMenuItemCommand(){
	encryptContent();
}

function myDump(aMessage) {
	  var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
	                                 .getService(Components.interfaces.nsIConsoleService);
	  consoleService.logStringMessage("CrytoBird: " + aMessage);
	}

/**
 * refreshes window title (manual input from user needed, so we send blank and del as keyboard events)
 */
function refreshTitle(){
	sendKeyEvent(32);
	sendKeyEvent(8);
}

/**
 * function needed for workaround to update title of window: this function inserts the character c into the title which causes the window title to change 
 * @param c  the character which should be inserted
 */

function sendKeyEvent(c){
	var e = document.createEvent("KeyboardEvent");
	e.initKeyEvent('keypress', true, true, null, false, false, false, false, c, c);
	
	var obj = document.getElementById("item-title");
	obj.inputField.dispatchEvent(e);
}